//
//  lookbookGalleryViewController.m
//  look.book
//
//  Created by Chih yuan Liu on 6/6/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "lookbookGalleryViewController.h"


@implementation lookbookGalleryViewController

@synthesize scrollView;

@synthesize delegate;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
    }
    return self;
}

- (void)dealloc
{
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

//generate content & layout
- (void) layoutContent{
	UIImageView *view = nil;
	NSArray *subviews = [scrollView subviews];

	int i = 0;
	for (view in subviews)
	{
		if ([view isKindOfClass:[UIView class]] && view.tag > 0)
		{
			CGFloat curYLoc = 250 * (i / 5) + 30;
			CGFloat curXLoc = 196 * (i % 5) + 30;
			CGRect frame = view.frame;
			frame.origin = CGPointMake(curXLoc, curYLoc);
			view.frame = frame;
			view.tag = (i+1);
            UIImageView* imageView = (UIImageView*)[view viewWithTag:999];
            imageView.tag = i;
			i++;
		}
	}
    
    int rowsheight = (i/6) +1;
    rowsheight += (i%6>0)?1:0;
	// set the content size so it can be scrollable
	[scrollView setContentSize:CGSizeMake( 1000,
                                          rowsheight* 280)];
	
}

-(void)addToContent:(UIView*)holder{
    UIView *view = nil;
	NSArray *subviews = [holder subviews];
    //NSLog(@"subviews before count:%d", [[holder subviews] count]);
	for (view in subviews)
	{
        [scrollView insertSubview:view atIndex:0];
    }
    //NSLog(@"subviews count:%d", [[holder subviews] count]);
    //NSLog(@"scrollview subviews count:%d", [[scrollView subviews] count]);
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
	return YES;
}


-(void)relayoutContent{
    [self layoutContent];
}

-(IBAction)dismiss:(id)sender{
    [self dismissModalViewControllerAnimated:TRUE];
}

@end
